#include <stdio.h>
#include <conio.h>
#include <string>
#define MAX 1000
void xuat_sach_vi_tri_i(int vitrisach);
char ISBN[MAX][100];
char tensach[MAX][100];
char tacgia[MAX][100];
char nhaxuatban[MAX][100];
char namxuatban[MAX][100];
char theloai[MAX][100];
char giasach[MAX][100];
char soquyansach[MAX][100];
int soluong_sach_HT=0;

void them_Sach(int &soluong_sach)
{
	int i;

	int soluong_sach_hientai_tren_mang = soluong_sach_HT;

	printf(" nhap vao so luong sach can them ");
	scanf("%d", &soluong_sach);
	for(i=0;i<soluong_sach;i++)
	{
		int vitriPhantuvaoMang = i + soluong_sach_hientai_tren_mang;

		fflush(stdin);
		printf(" Nhap ma ISBN sach %d: ",i+1);
		gets(ISBN[vitriPhantuvaoMang]);
		printf(" Nhap ten sach thu %d: ",i+1);
		gets(tensach[vitriPhantuvaoMang]);
		printf(" Nhap tac gia thu %d: ",i+1);
		gets(tacgia[vitriPhantuvaoMang]);
		printf(" Nhap nha suat ban sach thu %d: ",i+1);
		gets(nhaxuatban[vitriPhantuvaoMang]);
		printf(" Nhap nam xuat ban sach thu %d: ",i+1);
		gets(namxuatban[vitriPhantuvaoMang]);
		printf(" Nhap the loai sach thu  %d: ",i+1);
		gets(theloai[vitriPhantuvaoMang]);
		printf(" Nhap gia sach thu nhat %d: ",i+1);
		gets(giasach[vitriPhantuvaoMang]);
		printf(" Nhap so quyen sach thu %d: ",i+1);
		gets(soquyansach[vitriPhantuvaoMang]);
		soluong_sach_HT++;
	}
	printf(" Them Sach Thanh Cong!! Moi Ban Chon Chuc Nang Tiep Theo !! \n");
}

void xuat_tat_ca_sach()
{
	int i;
	for(i=0;i<soluong_sach_HT;i++)
	{
		printf(" sach Thu: %d \n",i+1);
		printf(" ISBN: %s \n",ISBN[i]);
		printf(" Ten Sach: %s \n",tensach[i]);
		printf(" Tac Gia: %s \n",tacgia[i]);
		printf(" Nha Xuat Ban: %s \n",nhaxuatban[i]);
		printf(" Nam Xuat Ban: %s \n",namxuatban[i]);
		printf(" The Loai: %s \n",theloai[i]);
		printf(" Gia Sach: %s \n",giasach[i]);
		printf("So Quyen: %s \n",soquyansach[i]);	
	}
}
void Sua_sach_Vi_Tri_Thu_I()
{
	    int i;
	    printf(" Ban muon sua sach thu may: ");
	    scanf("%d", &i);

		fflush(stdin);
		if(i<=0 || i > soluong_sach_HT)
		{
			printf("Khong tim thay sach ban muon sua ! \n" );
		}
		else
		{
			printf(" Nhap ISBN %d ", i );
			gets(ISBN[i-1]);
			printf(" Nhap Ten Sach %d: ",i);
			gets(tensach[i-1]);
			printf(" Nhap Tac Gia %d: ",i);
			gets(tacgia[i-1]);
			printf(" Nhap Nha Xuat Ban %d: ",i);
			gets(nhaxuatban[i-1]);
			printf(" Nhap Nam Xuat Ban %d: ",i);
			gets(namxuatban[i-1]);
			printf(" Nhap The Loai %d: ",i);
			gets(theloai[i-1]);
			printf(" Nhap Gia Sach %d: ",i);
			gets(giasach[i-1]);
			printf(" Nhap So Quyen Sach %d: ",i);
			gets(soquyansach[i-1]);
		}
}

void xoa_sach_vi_tri_I()
{
	int  i,k;
	printf(" Ban muon xoa quyen sach  thu may: ");
	scanf("%d",&k);
	if(k<=0 || k > soluong_sach_HT)
	{
		printf("Khong tim thay sach ! \n" );
	}
	else 
	{
		for(i=k;i<soluong_sach_HT;i++)
		{
			
			strcpy( ISBN[i-1],ISBN[i]);
			strcpy(tensach[i-1] , tensach[i]);
			strcpy(tacgia[i-1] , tacgia[i]);
			strcpy(nhaxuatban[i-1] , nhaxuatban[i]);
			strcpy(namxuatban[i-1], namxuatban[i]);
			strcpy(theloai[i-1], theloai[i]);
			strcpy(giasach[i-1], giasach[i]);
			strcpy(soquyansach[i-1], soquyansach[i]);
		}
		soluong_sach_HT--;
	}
}

void TK_sach_ISBN()
{
	int i; char x[MAX][100];
	fflush(stdin);
	printf(" Nhap vao ma sach ISBN can tim: ");
	gets(x[0]);
	bool isbn = false;
	int vitricantim = -1;
	for(i=0;i<soluong_sach_HT;i++)
	{

	   if( strcmp(ISBN[i],x[0]) == 0)
		{
			vitricantim = i;
			isbn = true;
			break;
		}
		
	}

	if(isbn == true)
	{
		printf("Tim Thanh Cong \n"); 
		xuat_sach_vi_tri_i(vitricantim);
	}
	else
	{
		printf("khong tim thay \n");
	}

}

void TK_sach_Ten()
{
	int i; char x[MAX][100];
	int mangtim[MAX] ;

	fflush(stdin);
	printf(" Nhap vao ten sach can tim: ");
	gets(x[0]);
    int dem = 0; 
	for(i=0;i<soluong_sach_HT;i++)
	{

	   if( strcmp(tensach[i],x[0]) == 0)
		{
			mangtim[dem] = i;
			dem++;
		}
	}

	if(dem > 0)
	{
		printf(" Tim Kiem Thanh Cong \n");
		int phantu;
		for(phantu = 0; phantu < dem ; phantu++)
		{
			xuat_sach_vi_tri_i(mangtim[phantu]);
		}
	}
	else
	{
		printf("khong tim thay \n");
	}
}

void KT_sach_loai()
{
	int i,j;
	for(i=0;i<soluong_sach_HT;i++)
	{
		int dem=0;
		for(j=0;j<soluong_sach_HT;j++)
		{
			if(strcmp(theloai[i],theloai[j])==0)
			{
				if(i<=j)
				{
					dem++;
				}
				else
				{
					break;
				}
			}
		}
		if(dem!=0)
		{
			printf("the loai %s: co %d quyen trong thu vien \n", theloai[i],dem);
		}
	}
}

void xuat_sach_vi_tri_i(int vitrisach)
{
		printf(" Ma ISBN: %s \n",ISBN[vitrisach]);
		printf(" Ten Sach: %s \n",tensach[vitrisach]);
		printf(" Tac Gia: %s \n",tacgia[vitrisach]);
		printf(" Nha Sach Ban: %s \n",nhaxuatban[vitrisach]);
		printf(" Nam Xuat Ban: %s \n",namxuatban[vitrisach]);
		printf(" The Loai: %s \n",theloai[vitrisach]);
		printf(" Gia Sach: %s \n",giasach[vitrisach]);
		printf("So Quyen Sach: %s \n",soquyansach[vitrisach]);
}


